# XBMC Video add-on for No Agenda Show (noagendastream.com)
# created by Ludolphus, januari 2011, ludolphus@gmail.com
# The No Agenda show is hosted by me
import urllib,urllib2,re,sys,xbmcplugin,xbmcgui,xbmcaddon,httplib

HEADER = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.9.0.3) Gecko/2008092417 Firefox/3.6.3'

def CATEGORIES():
	addDir('The Streams','streams',1,'http://img684.imageshack.us/img684/2161/mainphpg2viewcore.png')
	addDir('The Episodes (page 1)','http://www.mevio.com/episode/265843/?sId=15412&sOffset=0&sLimit=50&format=show-episodes',1,'http://static.podshow.com/profiles/BackgroundFiles/noagenda.jpg')
	addDir('The Episodes (page 2)','http://www.mevio.com/episode/265843/?sId=15412&sOffset=50&sLimit=50&format=show-episodes',1,'http://static.podshow.com/profiles/BackgroundFiles/noagenda.jpg')
	addDir('The Episodes (page 3)','http://www.mevio.com/episode/265843/?sId=15412&sOffset=100&sLimit=50&format=show-episodes',1,'http://static.podshow.com/profiles/BackgroundFiles/noagenda.jpg')
	addDir('The Episodes (page 4)','http://www.mevio.com/episode/265843/?sId=15412&sOffset=150&sLimit=50&format=show-episodes',1,'http://static.podshow.com/profiles/BackgroundFiles/noagenda.jpg')
	addDir('The Episodes (page 5)','http://www.mevio.com/episode/265843/?sId=15412&sOffset=200&sLimit=50&format=show-episodes',1,'http://static.podshow.com/profiles/BackgroundFiles/noagenda.jpg')

def INDEX(url):
	if (url=='streams'):
		addLink('Main Stream','http://live.noagendamix.com:8000/listen.pls',2,'','','http://img31.imageshack.us/img31/2161/mainphpg2viewcore.png')
		addLink('Primary Back-up Stream','http://38.96.148.80:8208/listen.pls',2,'','','http://img684.imageshack.us/img684/2161/mainphpg2viewcore.png')
		addLink('Secondary Back-up Stream','http://dsclive.primcast.com:8208/listen.pls',2,'','','http://img510.imageshack.us/img510/5088/nalogo.jpg')
		addLink('Low Bitrate Stream','http://lofi.noagendamix.com:8000/listen.pls',2,'','','http://img689.imageshack.us/img689/4329/mainphpg2viewcorel.png')
	else:
		req = urllib2.Request(url)
		req.add_header('User-Agent', HEADER)
		response = urllib2.urlopen(req)
		link=response.read()
		response.close()
#		items=re.compile('<li><div class="episode-thumb media-item-(.*)">\n(\s*?)<a href="(.*)" onmouseover(.*)\n(\s*?)<span(.*)</span>\n(\s*?)<img class="neg-img-margin-0" src="(.*)\?url=(.*)\n(.*)\n\t\t\n\t\t<div class="episode-description">\n\t\t\t<p class="date">\n\t\t\t<a href="(.*)">(.*)</a></p>\n\t\t\t<p>\n\t\t\t<a href="(.*)">(.*)</a></p>\n\t\t\t<p class="show-description">\n\t\t\t<a href="(.*)">(.*)</a>').findall(link)
		items=re.compile('<li><div class="episode-thumb media-item-(.*)">\n(\s*?)<a href="(.*)" onmouseover(.*)\n(\s*?)<span(.*)</span>\n(\s*?)<img class="neg-img-margin-0" src="(.*)" alt=(.*)\n(.*)\n\t\t\n\t\t<div class="episode-description">\n\t\t\t<p class="date">\n\t\t\t<a href="(.*)">(.*)</a></p>\n\t\t\t<p>\n\t\t\t<a href="(.*)">(.*)</a></p>\n\t\t\t<p class="show-description">\n\t\t\t<a href="(.*)">(.*)</a>').findall(link)

		for t1,t2,showurl,t3,t4,t5,t6,thumburl,t7,t8,t9,t10,t11,episode,t12,title in items:
			if (thumburl==''):
				thumburl='DefaultAudio.png'

			url='http://m.podshow.com/download_media/15412/episodes/'+showurl[29:35]+'/'+'noagenda-'+showurl[29:35]+'-'+showurl[48:50]+'-'+showurl[51:53]+'-'+showurl[43:47]+'.mp3'
			addLink(episode[3:6]+') '+title,url,2,'','',thumburl)

	return

def VIDEOLINKS(url):
    play=xbmc.Player().play(url)

def get_params():
        param=[]
        paramstring=sys.argv[2]
        if len(paramstring)>=2:
                params=sys.argv[2]
                cleanedparams=params.replace('?','')
                if (params[len(params)-1]=='/'):
                        params=params[0:len(params)-2]
                pairsofparams=cleanedparams.split('&')
                param={}
                for i in range(len(pairsofparams)):
                        splitparams={}
                        splitparams=pairsofparams[i].split('=')
                        if (len(splitparams))==2:
                                param[splitparams[0]]=splitparams[1]

        return param

def addLink(name,url,mode,date,desc,iconimage):
	u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
	ok=True
	liz=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
	description = desc + "\n \n Published: " + date
	liz.setInfo( type="Video", infoLabels={ "Title": name,"Plot":description,"Date": date } )
	ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz)
	return ok

def addDir(name,url,mode,iconimage):
        u=sys.argv[0]+"?url="+urllib.quote_plus(url)+"&mode="+str(mode)+"&name="+urllib.quote_plus(name)
        ok=True
        liz=xbmcgui.ListItem(name, iconImage=iconimage, thumbnailImage=iconimage)
        liz.setInfo( type="Video", infoLabels={ "Title": name } )
        ok=xbmcplugin.addDirectoryItem(handle=int(sys.argv[1]),url=u,listitem=liz,isFolder=True)
        return ok


params=get_params()
url=None
name=None
mode=None

try:
        url=urllib.unquote_plus(params["url"])
except:
        pass
try:
        name=urllib.unquote_plus(params["name"])
except:
        pass
try:
        mode=int(params["mode"])
except:
        pass

if mode==None or url==None or len(url)<1:
        print ("")
        CATEGORIES()

elif mode==1:
        print ""
        INDEX(url)

elif mode==2:
	print ""
	VIDEOLINKS(url)

xbmcplugin.endOfDirectory(int(sys.argv[1]))